import json

from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from django.views import View

from apps.user.models import JobInfo


class AddJobInfo(View):
    def post(self, request):
        data = request.body
        data_dict = json.loads(data)
        job_title = data_dict.get('job_title')
        job_category = data_dict.get('job_category')
        job_experience = data_dict.get('job_experience')
        job_monthly_pay = data_dict.get('job_monthly_pay')
        job_education = data_dict.get('job_education')
        job_address = data_dict.get('job_address')
        job_count = data_dict.get('job_count')
        job_major = data_dict.get('job_major')
        create_time = data_dict.get('create_time')

        if not all([job_title, job_category, job_experience, job_monthly_pay, job_education, job_address, job_count,
                    job_major, create_time]):
            return JsonResponse({"code": 400, "errmsg": "缺少参数"})

        try:
            job = JobInfo.objects.create(job_title=job_title, job_category=job_category,
                                         job_experience=job_experience, job_monthly_pay=job_monthly_pay,
                                         job_education=job_education, job_address=job_address, job_count=job_count,
                                         job_major=job_major, create_time=create_time, )
            # 密码加密   数据保存可能不成功捕获异常
        except Exception as e:
            print(e)
            return JsonResponse({'code': 400, 'errmsg': "就业信息保存失败"})

        response = JsonResponse({"code": 0, "errmsg": "ok"})
        return response


class ChangeJobInfo(View):
    def post(self, request, id):
        data = request.body
        data_dict = json.loads(data)
        job_title = data_dict.get('job_title')
        job_category = data_dict.get('job_category')
        job_experience = data_dict.get('job_experience')
        job_monthly_pay = data_dict.get('job_monthly_pay')
        job_education = data_dict.get('job_education')
        job_address = data_dict.get('job_address')
        job_count = data_dict.get('job_count')
        job_major = data_dict.get('job_major')
        # create_time = data_dict.get('create_time')

        if not all([job_title, job_category, job_experience, job_monthly_pay, job_education, job_address, job_count,
                    job_major]):
            return JsonResponse({"code": 400, "errmsg": "缺少参数"})

        try:
            job = JobInfo.objects.filter(id=id).update(job_title=job_title, job_category=job_category,
                                                       job_experience=job_experience, job_monthly_pay=job_monthly_pay,
                                                       job_education=job_education, job_address=job_address,
                                                       job_count=job_count,
                                                       job_major=job_major)
            # jobs = JobInfo.objects.create(job_title=job_title, job_category=job_category,
            #                              job_experience=job_experience, job_monthly_pay=job_monthly_pay,
            #                              job_education=job_education, job_address=job_address, job_count=job_count,
            #                              job_major=job_major, create_time=create_time, )
            # 密码加密   数据保存可能不成功捕获异常
        except Exception as e:
            print(e)
            return JsonResponse({'code': 400, 'errmsg': "就业信息修改失败"})

        response = JsonResponse({"code": 0, "errmsg": "ok"})
        return response


class DeleteJobInfo(View):
    def post(self, request, id):
        # data = request.body
        # data_dict = json.loads(data)
        # job_title = data_dict.get('job_title')
        # job_category = data_dict.get('job_category')
        # job_experience = data_dict.get('job_experience')
        # job_monthly_pay = data_dict.get('job_monthly_pay')
        # job_education = data_dict.get('job_education')
        # job_address = data_dict.get('job_address')
        # job_count = data_dict.get('job_count')
        # job_major = data_dict.get('job_major')
        # # create_time = data_dict.get('create_time')

        # if not all([job_title, job_category, job_experience, job_monthly_pay, job_education, job_address, job_count,
        #             job_major]):
        #     return JsonResponse({"code": 400, "errmsg": "缺少参数"})

        try:
            job = JobInfo.objects.get(id=id)
            job.delete()

            # jobs = JobInfo.objects.create(job_title=job_title, job_category=job_category,
            #                              job_experience=job_experience, job_monthly_pay=job_monthly_pay,
            #                              job_education=job_education, job_address=job_address, job_count=job_count,
            #                              job_major=job_major, create_time=create_time, )
            # 密码加密   数据保存可能不成功捕获异常
        except Exception as e:
            print(e)
            return JsonResponse({'code': 400, 'errmsg': "就业信息删除失败"})

        response = JsonResponse({"code": 0, "errmsg": "ok"})
        return response
